home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Think Class Libraries / CBartenderHelpMenu.p 1.0 / CBartenderHelpMenu.p
Encoding:
Text File  |  1996-01-10  |  11.9 KB  |  350 lines  |  [TEXT/PJMM]

  1. {****************************************************}
  2. {}
  3. {        CBartenderHelpMenu.p                                                                                                                                                                            }
  4. {}
  5. {        SUPERCLASS = CBartender                                                                                                                                                            }
  6. {}
  7. {        Copyright © 1995 Patrick C Hew. All rights reserved.                                                                        }
  8. {}
  9. {        CBartenderHelpMenu is a bartender which handles items in the application                }
  10. {        help menu. This is the part of the help menu below the standard help menu                }
  11. {        items. This class does not "own" the application help menu in any way. It                    }
  12. {        merely calls the appropriate Help Manager routines.                                                                                }
  13. {}
  14. {        If a item is selected from the help menu, the command value generated has                }
  15. {        a negative value. Following standard TCL practice, you should negate this                    }
  16. {        value and obtain the high and low words. The high word is the menu ID,                         }
  17. {        namely kHMHelpMenuID. However the low word is the position in the                                 }
  18. {        application help menu. This corrects for the presence of the standard items.            }
  19. {}
  20. {        Your DoCommand procedure for the object handling help could thus have –                }
  21. {}
  22. {        if HiWord(-theCommand) = kHMHelpMenuID then begin                                                                            }
  23. {                DoAppHelpMenuItem(LoWord(-theCommand));                                                                                            }
  24. {        end;                                                                                                                                                                                                                                }
  25. {}
  26. {        At time of writing, the number of standard items in the help menu may be                 }
  27. {        taken as constant.    If this changes     in the future, then this class will need to            }
  28. {        be revised.                                                                                                                                                                                                            }
  29. {}
  30. {        Important: You must override MakeBartender and assign gBartender to be an        }
  31. {        object of CBartenderHelpMenu type, not of CBartender, as follows –                                 }
  32. {}
  33. {        procedure CMyApp.MakeBartender;                                                                                                                                    }
  34. {}
  35. {            var                                                                                                                                                                                                                                }
  36. {                    theBartenderHM : CBartenderHelpMenu;                                                                                                        }
  37. {}
  38. {        begin                                                                                                                                                                                                                             }
  39. {            new(theBartenderHM);                                                                                                                                                                    }
  40. {            gBartender := theBartenderHM;                                                                                                                                            }
  41. {            theBartenderHM.IBartenderHelpMenu(MBARapp);                                                                                        }
  42. {        end;                                                                                                                                                                                                                                }
  43. {}
  44. {        Version History –                                                                                                                                                                                        }
  45. {}
  46. {        Version:    1.00 for TCL 1.1.2                                                                                                                                                        }
  47. {        Date:             06 January 1995                                                                                                                                                            }
  48. {        Author:        Patrick C Hew     <phew@ucc.gu.uwa.edu.au>                                                                                }
  49. {        Notes:            Initial release.                                                                                                                                                                        }
  50. {                                    Based on C source code by Mark Coniglio, and following some ideas        }
  51. {                                    from Ken Beath.                                                                                                                                                                }
  52. {}
  53. {****************************************************}
  54.  
  55.  
  56. unit CBartenderHelpMenu;
  57.  
  58. interface
  59.  
  60.     uses
  61.         TCL;
  62.  
  63.     type
  64.         CBartenderHelpMenu = object(CBartender)
  65.  
  66.                 { The number of standard items in the help menu, above the application help menu. }
  67.                 { This value is initialized when the object is initialized. At time of writing, the }
  68.                 { number of such items is constant. }
  69.                 numStandardHelpMenuItems: Integer;
  70.  
  71.                 { The number of items in the application help menu. This is what we are managing. }
  72.                 { At all times, we should have that }
  73.                 { numStandardHelpMenuItems + numApplicationHelpMenuItems = CountMItems(GetMHandle(kHMHelpMenuID)) }
  74.                 { That is, our record of the number of standard and application items in the help menu is }
  75.                 { actually what is there. }
  76.                 numApplicationHelpMenuItems: Integer;
  77.  
  78.                 { Initialize a bartender object which handles items in the application help menu. }
  79.                 procedure IBartenderHelpMenu (MBARid: integer);
  80.  
  81.                 { Find the command number corresponding to the specified Menu choice, }
  82.                 { as specified by a MENU resource ID and item number. }
  83.                 function FindCmdNumber (MENUid: Integer; itemNo: Integer): LongInt;
  84.                 override;
  85.  
  86.                 { Adds the requested items to the application help menu from a STR# resource. }
  87.                 { The procedure does not process the strings for metacharacters, except for }
  88.                 { processing a '-' into a dividing line. We disable the selectability of these lines. }
  89.                 procedure AddHelpMenuItems (resID: Integer; max: Integer);
  90.  
  91.                 { Inserts the specified string as a new item in the application help menu. }
  92.                 { The procedure does not process the strings for metacharacters, except for }
  93.                 { processing a '-' into a dividing line. We disable the selectability of these lines. }
  94.                 procedure Insert1HelpMenuItem (str: Str255; afterItem: Integer);
  95.  
  96.                 { Remove all items from the application help menu. }
  97.                 procedure RemoveHelpMenuItems;
  98.  
  99.                 { Remove the specified item from the application help menu. }
  100.                 procedure Remove1HelpMenuItem (itemNo: Integer);
  101.  
  102.             end; { CBartenderHelpMenu }
  103.  
  104.  
  105. implementation
  106.  
  107.  
  108. {****************************************************}
  109. {}
  110. {        IBartenderHelpMenu                                                                                                                                                                                }
  111. {}
  112. {        Initialize a bartender object which handles items in the help menu.                                        }
  113. {}
  114. {****************************************************}
  115.  
  116.     procedure CBartenderHelpMenu.IBartenderHelpMenu (MBARid: integer);
  117.  
  118.         var
  119.             help: MenuHandle;
  120.  
  121.     begin { IBartenderHelpMenu }
  122.         IBartender(MBARid);
  123.  
  124.         numStandardHelpMenuItems := 0;
  125.         numApplicationHelpMenuItems := 0;
  126.  
  127.         if gSystem.hasHelpMgr then begin
  128.  
  129.             { To count the number of standard items in the help menu, }
  130.             { we need a handle to the global help menu, not just the }
  131.             { application one. }
  132.  
  133.             help := GetMHandle(kHMHelpMenuID);
  134.  
  135.             { Note that the Help Manager adds a separating line between }
  136.             { the standard and application help menus. We include this }
  137.             { line in the count of the standard items. }
  138.  
  139.             if help <> nil then begin
  140.                 numStandardHelpMenuItems := CountMItems(help) + 1;
  141.             end; { if }
  142.         end; { if }
  143.     end; { IBartenderHelpMenu }
  144.  
  145.  
  146. {****************************************************}
  147. {}
  148. {        FindCmdNumber                                                                                                                                                                                                }
  149. {}
  150. {        Find the command number corresponding to the specified Menu choice,                            }
  151. {        as specified by a MENU resource ID and item number.                                                                                }
  152. {}
  153. {****************************************************}
  154.  
  155.     function CBartenderHelpMenu.FindCmdNumber (MENUid: Integer; itemNo: Integer): LongInt;
  156.  
  157.     begin { FindCmdNumber }
  158.         if gSystem.hasHelpMgr and (MENUid = kHMHelpMenuID) then begin
  159.             FindCmdNumber := -BSL(LongInt(MENUid), 16) - (itemNo - numStandardHelpMenuItems);
  160.         end { if }
  161.         else begin
  162.             FindCmdNumber := inherited FindCmdNumber(MENUid, itemNo);
  163.         end { else if }
  164.     end; { FindCmdNumber }
  165.  
  166.  
  167. {****************************************************}
  168. {}
  169. {        AddHelpMenuItems                                                                                                                                                                                    }
  170. {}
  171. {        The procedure does not process the strings for metacharacters, except for            }
  172. {        processing a '-' into a dividing line. We disable the selectability of these lines.    }
  173. {}
  174. {****************************************************}
  175.  
  176.     procedure CBartenderHelpMenu.AddHelpMenuItems (resID: Integer; max: Integer);
  177.  
  178.         var
  179.             i: Integer;
  180.             help: MenuHandle;
  181.             hcount: Integer;
  182.             str: Str255;
  183.             err: OSErr;
  184.  
  185.     begin { AddHelpMenuItems }
  186.         if gSystem.hasHelpMgr then begin
  187.  
  188.             err := HMGetHelpMenuHandle(help);
  189.  
  190.             if err = noErr then begin
  191.                 { Application help menu exists. Count the current number of items. }
  192.                 hcount := CountMItems(help);
  193.  
  194.                 for i := 1 to max do begin
  195.                     GetIndString(str, resID, i);
  196.  
  197.                     { We can't process for metacharacters, otherwise we might add multiple }
  198.                     { menus for a single string. However SetItem processes '-' into item }
  199.                     { separators, which we should disable. }
  200.  
  201.                     { Important – Add a blank string, not an empty one. }
  202.                     AppendMenu(help, ' ');
  203.                     SetItem(help, hcount + i, str);
  204.  
  205.                     numApplicationHelpMenuItems := numApplicationHelpMenuItems + 1;
  206.  
  207.                     if str = '-' then begin
  208.                         DisableItem(help, hcount + i)
  209.                     end { if }
  210.                     else begin
  211.                         EnableItem(help, hcount + i);
  212.                     end; { else }
  213.  
  214.                 end; { for }
  215.             end; { if }
  216.  
  217.             ASSERT(numStandardHelpMenuItems + numApplicationHelpMenuItems = CountMItems(GetMHandle(kHMHelpMenuID)));
  218.         end; { if }
  219.     end; { AddHelpMenuItems }
  220.  
  221.  
  222. {****************************************************}
  223. {}
  224. {        Add1HelpMenuItem                                                                                                                                                                                    }
  225. {}
  226. {        Inserts the specified string as a new item in the application help menu.                            }
  227. {        The procedure does not process the strings for metacharacters, except for            }
  228. {        processing a '-' into a dividing line. We disable the selectability of these lines.     }
  229. {}
  230. {****************************************************}
  231.  
  232.     procedure CBartenderHelpMenu.Insert1HelpMenuItem (str: Str255; afterItem: Integer);
  233.  
  234.         var
  235.             help: MenuHandle;
  236.             err: OSErr;
  237.  
  238.     begin { Insert1HelpMenuItem }
  239.         if gSystem.hasHelpMgr then begin
  240.  
  241.             err := HMGetHelpMenuHandle(help);
  242.  
  243.             if err = noErr then begin
  244.                 { Application help menu exists. }
  245.  
  246.                 { We can't process for metacharacters, otherwise we might add multiple }
  247.                 { menus for a single string. However SetItem processes '-' into item }
  248.                 { separators, which we should disable. }
  249.  
  250.                 { In spite of what is implied in the documentation, HMGetHelpMenuHandle }
  251.                 { actually appears to be a handle to the global help menu. To get the }
  252.                 { correct effect, we have to offset. }
  253.  
  254.                 { Important – Insert a blank string, not an empty one. }
  255.                 InsMenuItem(help, ' ', numStandardHelpMenuItems + afterItem);
  256.                 SetItem(help, numStandardHelpMenuItems + afterItem + 1, str);
  257.  
  258.                 numApplicationHelpMenuItems := numApplicationHelpMenuItems + 1;
  259.  
  260.                 if str = '-' then begin
  261.                     DisableItem(help, numStandardHelpMenuItems + afterItem + 1)
  262.                 end { if }
  263.                 else begin
  264.                     EnableItem(help, numStandardHelpMenuItems + afterItem + 1);
  265.                 end; { else }
  266.  
  267.             end; { if }
  268.  
  269.             ASSERT(numStandardHelpMenuItems + numApplicationHelpMenuItems = CountMItems(GetMHandle(kHMHelpMenuID)));
  270.         end; { if }
  271.     end; { Insert1HelpMenuItem }
  272.  
  273.  
  274. {****************************************************}
  275. {}
  276. {        RemoveHelpMenuItems                                                                                                                                                                        }
  277. {}
  278. {        Remove all items from the application help menu.                                                                                            }
  279. {}
  280. {****************************************************}
  281.  
  282.     procedure CBartenderHelpMenu.RemoveHelpMenuItems;
  283.  
  284.         var
  285.             help: MenuHandle;
  286.             hcount: Integer;
  287.             err: OSErr;
  288.  
  289.     begin { RemoveHelpMenuItems }
  290.         if gSystem.hasHelpMgr then begin
  291.  
  292.             err := HMGetHelpMenuHandle(help);
  293.  
  294.             if err = noErr then begin
  295.                 { Application help menu exists. Count the current number of items. }
  296.                 hcount := CountMItems(help);
  297.  
  298.                 while numApplicationHelpMenuItems > 0 do begin
  299.                     { Delete the last item. }
  300.                     DelMenuItem(help, hcount);
  301.  
  302.                     { Decrement the count. }
  303.                     hcount := hcount - 1;
  304.                     numApplicationHelpMenuItems := numApplicationHelpMenuItems - 1;
  305.                 end; { while }
  306.             end; { if }
  307.  
  308.             ASSERT(numStandardHelpMenuItems + numApplicationHelpMenuItems = CountMItems(GetMHandle(kHMHelpMenuID)));
  309.         end; { if }
  310.     end; { RemoveHelpMenuItems }
  311.  
  312.  
  313. {****************************************************}
  314. {}
  315. {        Remove1HelpMenuItem                                                                                                                                                                        }
  316. {}
  317. {        Remove the specified item from the application help menu.                                                                }
  318. {}
  319. {****************************************************}
  320.  
  321.     procedure CBartenderHelpMenu.Remove1HelpMenuItem (itemNo: Integer);
  322.  
  323.         var
  324.             help: MenuHandle;
  325.             err: OSErr;
  326.             macMenu: MenuHandle;
  327.             i, oldNumCmds: Integer;
  328.  
  329.     begin { Remove1HelpMenuItem }
  330.         if gSystem.hasHelpMgr and (numApplicationHelpMenuItems > 0) then begin
  331.  
  332.             err := HMGetHelpMenuHandle(help);
  333.  
  334.             if err = noErr then begin
  335.  
  336.                 { In spite of what is implied in the documentation, HMGetHelpMenuHandle }
  337.                 { actually appears to be a handle to the global help menu. To get the }
  338.                 { correct effect, we have to offset. }
  339.  
  340.                 DelMenuItem(help, numStandardHelpMenuItems + itemNo);
  341.  
  342.                 numApplicationHelpMenuItems := numApplicationHelpMenuItems - 1;
  343.             end; { if }
  344.  
  345.             ASSERT(numStandardHelpMenuItems + numApplicationHelpMenuItems = CountMItems(GetMHandle(kHMHelpMenuID)));
  346.         end; { if }
  347.     end; { Remove1HelpMenuItem }
  348.  
  349.  
  350. end. { CBartenderHelpMenu }